const Service = require('egg').Service;
// #f1d43b 文件夹颜色
class FolderService extends Service {

  /**
   * @description 根据id获取文件夹
   * @author xiezuobing[948466]<435321508@qq.com>
   * @param {any} folderId 
   * @returns  
   * @memberof FolderService
   */
  async selectById(folderId, name = '') {
    const ctx = this.ctx;
    return await ctx.model.Folder.findById(folderId, {
        include: [
          {
            model: ctx.model.Folder,
            as: 'parent',
            attributes: ['id', 'name']
          }, {
            model: ctx.model.Folder,
            as: 'grandParent',
            attributes: ['id', 'name']
          },
        ]
      });
  }

  /**
   * @description 查询所有的文件夹包括子文件夹
   * @author xiezuobing[948466]<435321508@qq.com>
   * @param {any} parentId 
   * @returns  
   * @memberof FolderService
   */
  async list(parentId = 0, name = '', deleteFlag = 0) {
    const ctx = this.ctx;
    return await ctx.model.Folder.findAll({
        where: {
          parent_id: parentId,
          name: {
            $like: '%' + name + '%'
          },
          delete_flag: deleteFlag
        },
      });
  }

  /**
   * @description 创建
   * @author xiezuobing[948466]<435321508@qq.com>
   * @param {any} folder 
   * @returns  
   * @memberof FolderService
   */
  async create(folder) {
    return await this.ctx.model.Folder.create(folder);
  }

  /**
   * @description 销毁记录
   * @author xiezuobing[948466]<435321508@qq.com>
   * @param {any} folderId 
   * @returns  
   * @memberof FolderService
   */
  async delete(folderId) {
    return await this.ctx.model.Folder.destroy({
        where: {
          $or: [
            {
              id: folderId,
            },
            {
              parent_id: folderId
            },
            {
              grand_parent_id: folderId
            }
          ]
        }
      });
  }


  /**
   * @description 更新
   * @author xiezuobing[948466]<435321508@qq.com>
   * @param {any} values 
   * @returns  
   * @memberof FolderService
   */
  async update(values) {
    return await this.ctx.model.Folder.update(values, {
        where: {
          id: values.id
        }
      })
  }

}

module.exports = FolderService;